{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Necesary conditions for Jobsequencing\n",
    "### Install pyQUBO from Recruit Communications Co. Ltd.\n",
    "    pip install pyqubo\n",
    "### Install openJij from Jij Inc.  (startup from Tohoku University)\n",
    "    pip install open jij\n",
    "### Install dwave-ocean-sdk from D-Wave Systems Inc.\n",
    "    pip install dwave-ocean-sdk\n",
    "    dwave config create\n",
    "    Profile (create new) [prod]: prod\n",
    "    API endpoint URL [skip]: (Enter)\n",
    "    Authentication token [skip]:\n",
    "    Default client class (qpu or sw) [qpu]: (Enter)\n",
    "    Default solver [skip]: (Enter)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# d-wave solves number partition"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### import pyQUBO, openJij and numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyqubo  import Array,Constraint, Placeholder\n",
    "import openjij as jij\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Array, Constrains and Placeholders are convenient classes from pyQUBO"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Prepare spin variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "N = 8\n",
    "vartype = \"SPIN\"\n",
    "x = Array.create(\"x\",shape=N,vartype=vartype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\"x\" is name of variables  \n",
    "shape specifies the shape of variables as vector, matrix, or...  \n",
    "vartype selects -1 or 1 by \"SPIN\" and 0 or 1by \"BINARY\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Array([Spin(x[0]), Spin(x[1]), Spin(x[2]), Spin(x[3]), Spin(x[4]), Spin(x[5]), Spin(x[6]), Spin(x[7])])\n"
     ]
    }
   ],
   "source": [
    "print(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Let generate integers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "n = np.linspace(1,N,N)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1. 2. 3. 4. 5. 6. 7. 8.]\n"
     ]
    }
   ],
   "source": [
    "print(n)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Define cost function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "E_cost = (np.sum(n*x))**2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Compile the cost function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = E_cost.compile()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Get qubo matrix"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "h,J, offset = model.to_ising()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Prepare quantum annealer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dwave.system.samplers import DWaveSampler\n",
    "sampler = DWaveSampler(solver=\"DW_2000Q_6\",token=\"cbb379a5b3a8cdb257c97bafae0ce600bd26627\")\n",
    "V = sampler.nodelist\n",
    "A = sampler.edgelist"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "V is a node set called from the current situation in the D-Wave 2000Q.  \n",
    "A is an edge set called from the current situation in the D-Wave 2000Q."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Find embedding on the chimera graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [],
   "source": [
    "adjacency = {}\n",
    "for i in J.keys():\n",
    "    adjacency[i] = 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here adjacency detects non-zero interactions in the matrix J."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "from minorminer import find_embedding \n",
    "embedding = find_embedding(adjacency, A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'x[0]': [318, 310, 304], 'x[1]': [309, 317], 'x[2]': [438, 306, 446, 434], 'x[3]': [442, 314], 'x[4]': [316, 308, 313], 'x[5]': [443, 315, 445], 'x[6]': [440, 447, 312], 'x[7]': [311, 307, 319]}\n"
     ]
    }
   ],
   "source": [
    "print(embedding)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Embed the problem onto  the chimera graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dwave.system.composites import FixedEmbeddingComposite\n",
    "sampler = FixedEmbeddingComposite(sampler, embedding=embedding)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Perform quantum annealing by D-Wave 2000Q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [],
   "source": [
    "response =  sampler.sample_ising(h,J, postprocess=\"optimization\",num_reads=1000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "postprocess has three choices as \"optimization\", \"sampling\", and \"None\".  \n",
    "num_reads denotes the number of iterations of quantum annealing."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Show results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7] energy num_oc. chain_.\n",
      "0   +1   +1   +1   -1   +1   -1   -1   +1 -200.0     189    0.75\n",
      "1   -1   -1   -1   +1   -1   +1   +1   -1 -200.0     313    0.75\n",
      "3   +1   +1   +1   -1   +1   -1   -1   +1 -200.0     185    0.75\n",
      "2   -1   -1   +1   +1   -1   +1   +1   -1 -188.0     313    0.75\n",
      "['SPIN', 4 rows, 1000 samples, 8 variables]\n"
     ]
    }
   ],
   "source": [
    "print(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "spin = response.record[\"sample\"][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19.0 36.0\n"
     ]
    }
   ],
   "source": [
    "temp = 0\n",
    "for k in range(N):\n",
    "    if spin[k]>0:\n",
    "        temp += n[k]\n",
    "print(temp,n.sum())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
